package com.cangxuan.app.ui.user;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import com.alibaba.fastjson.JSONObject;
import com.cangxuan.app.R;
import com.cangxuan.app.entity.User;
import com.cangxuan.app.ui.BaseActivity;
import com.cangxuan.app.ui.LoginActivity;
import com.cangxuan.app.utils.DeviceUtils;
import com.cangxuan.app.utils.HttpUtils;
import com.cangxuan.app.utils.SecurityUtils;
import com.cangxuan.app.utils.UserUtils;

import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Map;

/**
 * Author: 陈青石、杨刚
 * Activity: 交易密码页面
 * Layout: activity_set_pay_password
 */
public class SetPayPasswordActivity extends BaseActivity implements View.OnClickListener {

    int size = 0;
    RadioButton radiobutton1;
    RadioButton radiobutton2;
    RadioButton radiobutton3;
    RadioButton radiobutton4;
    RadioButton radiobutton5;
    RadioButton radiobutton6;
    Button bt0;
    Button bt1;
    Button bt2;
    Button bt3;
    Button bt4;
    Button bt5;
    Button bt6;
    Button bt7;
    Button bt8;
    Button bt9;

    String password = "";
    String password0 = "";
    String password1 = "";
    String password2 = "";
    TextView payPasswordTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_pay_password);
        setTitle("交易密码");
        radiobutton1 = findViewById(R.id.radiobutton1);
        radiobutton2 = findViewById(R.id.radiobutton2);
        radiobutton3 = findViewById(R.id.radiobutton3);
        radiobutton4 = findViewById(R.id.radiobutton4);
        radiobutton5 = findViewById(R.id.radiobutton5);
        radiobutton6 = findViewById(R.id.radiobutton6);
        payPasswordTitle = findViewById(R.id.paypassword_title);
        User user = UserUtils.getUser(context);
        if (user.isHasSetTransPassword()) {
            payPasswordTitle.setText("请输入原密码");
        } else {
            payPasswordTitle.setText("Step1 设置交易密码");
        }
        bt0 = findViewById(R.id.bt0);
        bt1 = findViewById(R.id.bt1);
        bt2 = findViewById(R.id.bt2);
        bt3 = findViewById(R.id.bt3);
        bt4 = findViewById(R.id.bt4);
        bt5 = findViewById(R.id.bt5);
        bt6 = findViewById(R.id.bt6);
        bt7 = findViewById(R.id.bt7);
        bt8 = findViewById(R.id.bt8);
        bt9 = findViewById(R.id.bt9);
        bt0.setOnClickListener(this);
        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
        bt3.setOnClickListener(this);
        bt4.setOnClickListener(this);
        bt5.setOnClickListener(this);
        bt6.setOnClickListener(this);
        bt7.setOnClickListener(this);
        bt8.setOnClickListener(this);
        bt9.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt0:
                changeRadioButton(0);
                break;
            case R.id.bt1:
                changeRadioButton(1);
                break;
            case R.id.bt2:
                changeRadioButton(2);
                break;
            case R.id.bt3:
                changeRadioButton(3);
                break;
            case R.id.bt4:
                changeRadioButton(4);
                break;
            case R.id.bt5:
                changeRadioButton(5);
                break;
            case R.id.bt6:
                changeRadioButton(6);
                break;
            case R.id.bt7:
                changeRadioButton(7);
                break;
            case R.id.bt8:
                changeRadioButton(8);
                break;
            case R.id.bt9:
                changeRadioButton(9);
                break;
        }
    }

    private void changeRadioButton(int x) {
        if (size == 0) {
            size++;
            radiobutton1.setChecked(true);
            password += x;
        } else if (size == 1) {
            size++;
            radiobutton2.setChecked(true);
            password += x;
        } else if (size == 2) {
            size++;
            radiobutton3.setChecked(true);
            password += x;
        } else if (size == 3) {
            size++;
            radiobutton4.setChecked(true);
            password += x;
        } else if (size == 4) {
            size++;
            radiobutton5.setChecked(true);
            password += x;
        } else if (size == 5) {
            size++;
            radiobutton6.setChecked(true);
            password += x;
            cleanFirstInput();
        }
    }
    Handler handler = new Handler();
    ProgressDialog progressDialog;

    private void cleanFirstInput() {
        size = 0;
        radiobutton1.setChecked(false);
        radiobutton2.setChecked(false);
        radiobutton3.setChecked(false);
        radiobutton4.setChecked(false);
        radiobutton5.setChecked(false);
        radiobutton6.setChecked(false);
        User user = UserUtils.getUser(context);
        String mobile = user.getMobile();
        System.out.println(mobile);
        if (user.isHasSetTransPassword() && password0.equals("")) {
            password0 = password;
            password = "";
            payPasswordTitle.setText("Step1 修改交易密码");
            return;
        }
         if (password1.equals("") && password2.equals("")) {
            password1 = password;
            password = "";
            payPasswordTitle.setText("Step2 确认交易密码");
        } else if (!(password1.equals("")) && password2.equals("")) {
            password2 = password;
            password = "";
            if (!password1.equals(password2)) {
                payPasswordTitle.setText("Step1 设置交易密码");
                password1 = "";
                password2 = "";
                Toast toast = Toast.makeText(getApplicationContext(), "两次密码不一致请重新输入", Toast.LENGTH_SHORT);
                toast.show();
            } else {
                progressDialog = ProgressDialog.show(this, "", "修改支付密码中请稍等...");
                if (password0.equals(password2)) {
                    Toast toast = Toast.makeText(getApplicationContext(), "新密码与原密码相同", Toast.LENGTH_SHORT);
                    toast.show();
                }
                Thread thread = new Thread(() -> {
                    //构建header
                    Map<String, String> headers = new HashMap<>();
                    headers.put("Authorization", DeviceUtils.getDeviceID());
                    JSONObject jsonBody = new JSONObject();
                    if(!password0.equals("")){
                    jsonBody.put("oldTransPassword", SecurityUtils.md5(mobile + password0));
                    }
                    jsonBody.put("transPassword", SecurityUtils.md5(mobile + password2));
                    String jsonString = HttpUtils.post("/api/user/changeTransPassword", headers, jsonBody.toJSONString());
                    JSONObject jsonResponse = JSONObject.parseObject(jsonString);
                    if (jsonResponse.getInteger("value") > 0) {
                        handler.post(() -> {
                            user.setHasSetTransPassword(true);
                            UserUtils.setUser(context, user);   //保存用户信息到本地
                            progressDialog.dismiss();
                            Toast toast = Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT);
                            toast.show();
                            Intent intent = new Intent();
                            intent.putExtra("user", user);
                            setResult(2, intent);
                            finish();
                        });
                    } else {
                        handler.post(() -> {
                            progressDialog.dismiss();
                            Toast toast = Toast.makeText(this, "修改失败,原密码输入错误", Toast.LENGTH_LONG);
                            toast.show();
                            finish();
                        });
                    }
                });
                thread.start();
            }
        }
    }
}
